package com.yzy.util.algorithm;

import at.favre.lib.crypto.bcrypt.BCrypt;
import com.yzy.properties.GlobalProperties;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class BCryptPassword {

    private static com.yzy.properties.GlobalProperties globalProperties;

    public BCryptPassword(GlobalProperties globalProperties) {
        BCryptPassword.globalProperties = globalProperties;
    }

    public static String encodePassword(String password) {
        byte[] bCryptPwd = BCrypt.withDefaults().hash(globalProperties.getPwdCost(), globalProperties.getPwdSalt().getBytes(), password.getBytes());
        return new String(bCryptPwd, StandardCharsets.UTF_8);
    }

    public static Boolean decodePassword(String rawPassword, String decodedPassword) {
        return BCrypt.verifyer().verify(rawPassword.toCharArray(), decodedPassword).verified;
    }
}